package edu.neusoft.android.yuejian14.model;

import java.util.List;

import edu.neusoft.android.yuejian14.Listener.RetrofitListener;
import edu.neusoft.android.yuejian14.javabean.Pic;
import edu.neusoft.android.yuejian14.service.RetrofitService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static edu.neusoft.android.yuejian14.Const_Service.BASE_URL;

public class PicModel {
    private Retrofit retrofit;
    public PicModel(){
        //创建实例
        retrofit =new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    //晒图列表
    public void  cardlist(final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<Pic>> call = retrofitService.cardlist();
        Callback<List<Pic>> callback = new Callback<List<Pic>>() {
            @Override
            public void onResponse(Call<List<Pic>> call, Response<List<Pic>> response) {
                List<Pic> pic = response.body();
                listener.onSuccess(pic,1);
            }
            @Override
            public void onFailure(Call<List<Pic>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

//晒图详情
public void getCardById(String cardid,final RetrofitListener listener) {
    RetrofitService retrofitService=retrofit.create(RetrofitService.class);
    Call call = retrofitService.getCardById(String.valueOf(cardid));
    call.enqueue(new Callback<Pic>() {
        @Override
        public void onResponse(Call<Pic> call, Response<Pic> response) {
            Pic pic=response.body();
            listener.onSuccess(pic,1);
        }
        @Override
        public void onFailure(Call<Pic> call, Throwable t) {
            listener.onFail();
        }
    });
}


}
